package com.greet.controller.admin;

import com.greet.context.BaseContext;
import com.greet.dto.OrdersPageQueryDTO;
import com.greet.entity.Orders;
import com.greet.result.PageResult;
import com.greet.result.Result;
import com.greet.service.OrderService;
import com.greet.vo.OrderSimplicityDetailVO;
import com.greet.vo.OrderStatisticsVO;
import com.greet.vo.OrderVO;
import com.greet.vo.OrdersHistoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController("adminController")
@RequestMapping("/admin/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/details/{id}")
    public Result<OrderVO> details(@PathVariable Long id){
        OrderVO orderVO = orderService.getById(id);
        orderVO.setUserId(BaseContext.getCurrentId());
        return Result.success(orderVO);
    }

    @GetMapping("/conditionSearch")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO){
        PageResult pageResult = orderService.conditionSearch(ordersPageQueryDTO);

        return Result.success(pageResult);
    }

    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> statistics(){
        OrderStatisticsVO statisticsVO = orderService.statistics();
        return Result.success(statisticsVO);
    }

    @PutMapping("/confirm")
    public Result confirm(@RequestBody Orders orders){

        try {
            orderService.confirm(orders.getId());
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success();
    }

    @PutMapping("/delivery/{id}")
    public Result delivery(@PathVariable Long id){
        orderService.delivery(id);
        return Result.success();
    }

    @PutMapping("/rejection")
    public Result rejection(@RequestBody Orders orders){
        try {
            orderService.rejection(orders);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success();
    }

    @PutMapping("/cancel")
    public Result cancel(@RequestBody Orders orders){
        orderService.cancel(orders);
        return Result.success();
    }

    @PutMapping("/complete/{id}")
    public Result complete(@PathVariable Long id){
        orderService.complete(id);
        return Result.success();
    }

}
